//! @Author: DengLibin 榆霖
//! @Date:  2022-06-30 15:21:22
//! @Description: 初始化相关

use std::thread;

/// @Author: DengLibin 榆霖
/// @Date:  2022-04-20 19:31:02
/// @Description: 初始化

pub const  TEMP_DIR: &str = "./tmp";
pub fn init() {
    init_log();
    init_sys();
}

/// @Author: DengLibin 榆霖
/// @Date:  2022-06-30 15:22:25
/// @Description: 日志初始化 log基本为Rust项目日志需求的标配库，env_logger提供了具体实现
fn init_log() {
    use chrono::Local;
    use std::io::Write;

    let env = env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "trace");
    env_logger::Builder::from_env(env)
        //格式
        .format(|buf, record| {
            //level添加颜色
            let level = { buf.default_styled_level(record.level()) };
            writeln!(
                buf,
                "{} {} [{}] [{}:{}] {}",
                Local::now().format("%Y-%m-%d %H:%M:%S"),
                level,
                thread::current().name().unwrap(),
                record.module_path().unwrap_or("<unnamed>"),
                record.line().unwrap_or(0), //行号
                &record.args()
            )
        })
        .init();
    info!("初始化日志完成")
}

/// @Author: DengLibin 榆霖
/// @Date:  2022-07-06 18:49:12
/// @Description: 创建文件夹
fn init_sys() {
    if let Ok(()) = std::fs::create_dir_all(TEMP_DIR){
        info!("创建临时目录:[{}]成功", TEMP_DIR);
    }else {
        error!("创建临时目录:[{}]失败", TEMP_DIR);
    }
}
